Components.utils.import("resource://lucidity/lucidapi.jsm");

var lucidctx = new (lucidapi.CryptoOperator.extend(function(classref) { return {
    initialize: function()
    {
        classref.superClass.initialize.call(this,window);
    }
}}));

lucidapi.absorb(lucidctx,{

    handleInplaceResult: function( resultobj )
    {
        var stuff = resultobj.m_result;
        var sigdata = resultobj.m_siginfo;
        if (stuff)
        {
            setSelectedText(stuff);
        }

        updateSignatureStatus(sigdata);
        return true;
    },

    getInvocationTarget: function()
    {
        var text = document.getElementById('text')
        var value = text.value;
        value = value.substring(text.selectionStart, text.selectionEnd)
        if (value == "")
            value = text.value;
        return value;
    }

});

function setSelectedText( text )
{
    var txtbox = document.getElementById('text');
    var startPos = txtbox.selectionStart;
    var endPos = txtbox.selectionEnd;
    var chaine = txtbox.value;
    if (startPos != endPos)
    {
        txtbox.value = chaine.substring(0, startPos) + text + chaine.substring(endPos, chaine.length);
    }
    else
    {
        txtbox.value = text;
    }
}
function loadStuff()
{
    try
    {
        var data = lucidapi.loadThingWithPicker(window);
        if (data)
            document.getElementById('text').value = data;
    }
    catch (e)
    {
        lucidapi.promptError(e);
    }
}

function saveStuff()
{
    var data = document.getElementById('text').value;
    lucidapi.saveThingWithPicker(window, data);
}

function copyStuff()
{
    var data = document.getElementById('text').value;
    var gClipboardHelper = lucidapi.getXpcomService("@mozilla.org/widget/clipboardhelper;1", "nsIClipboardHelper");
    gClipboardHelper.copyString(data);
}

function pasteStuff()
{
    var clip = lucidapi.getXpcomService("@mozilla.org/widget/clipboard;1", "nsIClipboard");
    if (!clip)
        return false;
    var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces["nsITransferable"]);
    if (!trans)
        return false;
    trans.addDataFlavor("text/unicode");

    clip.getData(trans, clip.kGlobalClipboard);
    var str = new Object();
    var strLength = new Object();
    trans.getTransferData("text/unicode", str, strLength);

    if (str)
        str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
    if (str)
    {
        var pastetext = str.data.substring(0, strLength.value / 2);
        setSelectedText(pastetext);
    }
    return true;
}

function clearStuff()
{
    document.getElementById('text').value = "";
}

function selectAllStuff()
{
    document.getElementById('text').select();
}

function updateSignatureStatus(sigdata)
{
    var sigel = document.getElementById('sigresult-statuspanel');
    if (sigdata)
    {
        sigel.label = lucidapi.formatSignatureResultMessage(sigdata);
    }
    else
    {
        sigel.label = 'none detected';
    }
}

this.onLoad = function(win)
{
    if (win.arguments == undefined)
        return;

    var optopts = win.arguments[0];

    document.getElementById('text').value = optopts.text;
    document.title = optopts.title;
    document.getElementById('lucidity-popup').addEventListener("popupshowing", lucidctx.updateObservers, false);

    updateSignatureStatus(optopts.siginfo);
}

